package org.skydiveapphd;

import org.skydiveapphd.interfaces.FreefallProfileTypeNotifier;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.FreefallProfileType;
import org.skydiveapplibrary.ui.FreefallProfileTypeSelectListAdapter;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class FreefallProfileTypeSelectListActivity extends BaseListFragment
{
	public static FreefallProfileTypeSelectListActivity newInstance(Bundle args) 
	{
		FreefallProfileTypeSelectListActivity f = new FreefallProfileTypeSelectListActivity();
		f.setArguments(args);
		return f;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	
    	// init view
    	getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    	setHeaderText(R.string.select_freefall_profile_type_label);
    	setTopButtonVisibility(View.GONE);
    	setBottomButtonVisibility(View.GONE);
    	
    	// init listview
    	FreefallProfileTypeSelectListAdapter adapter = new FreefallProfileTypeSelectListAdapter(getActivity());
		setListAdapter(adapter);
		
		// set initial selection
		FreefallProfileType selectedType = getCurrentSelection();
		for (int index = 0; index < adapter.getCount(); index++)
		{
			getListView().setItemChecked(index,
					adapter.getItems().get(index) == selectedType); 
		}
    }
    
    @Override
	public void onListItemClick(ListView listView, View view, int indx, long id)
    {
    	// get current selection
    	FreefallProfileType selectedType = getCurrentSelection();
    	// get new selection
    	FreefallProfileTypeSelectListAdapter adapter = (FreefallProfileTypeSelectListAdapter)getListAdapter();
		FreefallProfileType newSelectedType = (FreefallProfileType)adapter.getItem(indx);
    		
    	FreefallProfileTypeNotifier f = (FreefallProfileTypeNotifier) getFragmentManager().findFragmentByTag(getArguments().getString(Constants.EXTRA_FRAG_TAG));
    	
    	// if different than previous
    	if (selectedType != newSelectedType)
    		f.updateFreefallProfileType(newSelectedType.toString());
    	
		getFragmentManager().popBackStack();
    }
    
    private FreefallProfileType getCurrentSelection()
    {
    	String typeStr = this.getArguments().getString(Constants.EXTRA_FREEFALL_PROFILE_TYPE);
		return (typeStr != null) ? FreefallProfileType.valueOf(typeStr) : null;
    }
} 